﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Admin_MGift : System.Web.UI.Page
{
    GiftBUS giftBUS = new GiftBUS();
    DataTable tbGift = new DataTable();
    DataView viewGift;
    GCatalogBUS gCaBUS = new GCatalogBUS();
    DataTable tbGCatalog = new DataTable();
    List<GiftDetails> lstGift;
    DataTable tbdrlGCatalog = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            MultiView1.ActiveViewIndex = 0;
            LoadView2();
        }
        LoadData();
    }

    public void LoadData()
    {
        lstGift = new List<GiftDetails>();
        tbGift = giftBUS.returnAllGift();
        viewGift = new DataView(tbGift);
        viewGift.RowFilter = "G_Status=1";
        for (int i = 0; i < viewGift.Count; i++)
        {
            tbGCatalog = gCaBUS.returnCGiftByID(Int32.Parse(viewGift[i]["CG_ID"].ToString()));
            if (tbGCatalog.Rows.Count > 0)
            {
                GiftDetails item = new GiftDetails(viewGift[i]["G_ID"].ToString(), viewGift[i]["G_Name"].ToString(), float.Parse(viewGift[i]["G_Price"].ToString()), viewGift[i]["G_Details"].ToString(), viewGift[i]["G_Image"].ToString(), tbGCatalog.Rows[0]["CG_Name"].ToString());
                lstGift.Add(item);
            }
        }

        grvGift.DataSource = lstGift;
        grvGift.DataBind();

    }
    public void LoadView2()
    {
        tbdrlGCatalog = gCaBUS.returnAllGCatalog();

        drlCatalog.DataSource = tbdrlGCatalog;
        drlCatalog.DataTextField = "CG_Name";
        drlCatalog.DataValueField = "CG_ID";
        drlCatalog.DataBind();
    }
    protected void grvGift_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("DelItem"))
        {
            bool check = giftBUS.updateStatusGift(e.CommandArgument.ToString());
            if (check)
            {
                LoadData();
            }
        }
        if (e.CommandName.Equals("UpdateItem"))
        {
            MultiView1.ActiveViewIndex = 1;

            foreach (GiftDetails gift in lstGift)
            {
                if (gift.Id.Equals(e.CommandArgument.ToString()))
                {
                    txtID.Text = gift.Id;
                    txtName.Text = gift.Name;
                    txtDetails.Text = gift.Details;
                    txtPrice.Text = gift.Price.ToString();
                    imImage.ImageUrl = gift.Image;
                    lblUrl.Text = gift.Image;

                    for (int i = 0; i < drlCatalog.Items.Count; i++)
                    {
                        if (drlCatalog.Items[i].Text.Equals(gift.Cid))
                        {
                            drlCatalog.SelectedIndex = i;
                        }
                    }
                }
            }
        }
    }
    protected void grvGift_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvGift.PageIndex = e.NewPageIndex;
        LoadData();
    }
    
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        string id = "";
        string name = "";
        float price = 0;
        string details = "";
        string image = "";
        int cid = 0;        
        string path = Server.MapPath("..");

        if (txtName.Text.Equals("") || txtPrice.Text.Equals("") || txtDetails.Text.Equals(""))
        {
            lblError.Text = "- You must enter enough information";
        }
        else
        {
            id = txtID.Text;
            name = txtName.Text;
            price = float.Parse(txtPrice.Text);
            details = txtDetails.Text;
            if (FileUpload1.HasFile)
            {
                try
                {
                    image = "../images/" + FileUpload1.FileName;
                    FileUpload1.SaveAs(path + "\\images\\" + FileUpload1.FileName);
                }
                catch (Exception ex)
                {
                    lblError.Text = "- Upload file error";
                }

            }
            else
            {
                image = lblUrl.Text;
            }
            cid = Int32.Parse(drlCatalog.SelectedValue);
            bool check = giftBUS.updateGift(id, name, price, details, image, cid);
            if (check)
            {
                lblError.Text = "- Update Gift Success";
            }
            else
            {
                lblError.Text = "- Update Gift Error";
            }
        }

    }
    protected void lbnSearch_Click(object sender, EventArgs e)
    {
        int count = 0;
        List<GiftDetails> temp = new List<GiftDetails>();
        if (txtFindValue.Value.Length > 0)
        {
            foreach (GiftDetails gift in lstGift)
            {
                if (gift.Id.Equals(txtFindValue.Value))
                {
                    temp.Add(gift);
                }
                else
                {
                    count++;
                }
            }
            if (count == lstGift.Count)
            {
                grvGift.DataSource = null;
                grvGift.DataBind();
            }
            else
            {
                grvGift.DataSource = temp;
                grvGift.DataBind();
            }
        }
    }
}